//
//  YZPigDocHealthVC.m
//  VET
//
//  Created by YZ on 2018/7/19.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigDocHealthVC.h"
#import "YZPigDocHealthViewModel.h"
#import "YZPigDocHealthCell.h"
#import "YZBaseRefreshTableView.h"
#import "YZPigDocHealthDetailVC.h"

@interface YZPigDocHealthVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) YZPigDocHealthViewModel *viewModel;

@property (nonatomic, strong) YZBaseRefreshTableView *contentTb;

@end

@implementation YZPigDocHealthVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self addObserver];
    [self.contentTb headerBeginRefresh];
    
    // 删除后更新
    @weakify(self)
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:kHealthRefreshNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) {
        
        // 获取新数据
        @strongify(self)
        [self.contentTb headerBeginRefresh];
    }];
}

- (void)configUI
{
    [self.view addSubview:self.contentTb];
    
    YZWeakSelf(self);
    self.contentTb.headerRefreshBlock = ^{
        [weakself.contentTb resetState];
        [weakself.viewModel headerRefreshSuccess:^(id obj) {
            NSLog(@"obj:%ld",((NSArray *)obj).count);
            [weakself.contentTb reloadData];
            [weakself.contentTb headerEndRefresh];
        } fail:^(NSError *error) {
            [weakself.contentTb headerEndRefresh];
        }];
    };
    
    self.contentTb.footerRefreshBlock = ^{
        [weakself.viewModel footerRefreshSuccess:^(NSArray *obj) {
            if (obj.count == 0 && [weakself.viewModel isNoMoreData]) {
                [weakself.contentTb showNoMoreData];
                return;
            }
            [weakself.contentTb reloadData];
            [weakself.contentTb footerEndRefresh];
        } fail:^(NSError *error) {
            [weakself.contentTb footerEndRefresh];
        }];
    };
}

- (void)addObserver
{
    [RACObserve(self.viewModel, loading) subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            [SVProgressHUD show];
        }else
        {
            [SVProgressHUD dismiss];
        }
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataSource[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    static NSString *cellId = @"YZPigDocHealthCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[YZPigDocHealthCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    ((YZPigDocHealthCell *)cell).model = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZPigDocHealthDetailVC *healthDetailVC = [YZPigDocHealthDetailVC new];
    healthDetailVC.healthModel = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
    healthDetailVC.model = self.model;
    [self.navigationController pushViewController:healthDetailVC animated:YES];
}

#pragma mark -- setters && getters

- (YZBaseRefreshTableView *)contentTb
{
    if(!_contentTb){
        _contentTb = [[YZBaseRefreshTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopViewHeight - YZ_STATUSBAR_HEIGHT - kCurrentDeviceNum(50) - kCurrentDeviceNum(20) - YZ_BOTTOM_MARGIN) style:UITableViewStylePlain];
        _contentTb.tableFooterView = [UIView new];
        _contentTb.delegate = self;
        _contentTb.dataSource = self;
        _contentTb.estimatedRowHeight = 30;
        _contentTb.estimatedSectionHeaderHeight = 0;
        _contentTb.estimatedSectionFooterHeight = 0;
    }
    return _contentTb;
}

- (YZPigDocHealthViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [YZPigDocHealthViewModel new];
        _viewModel.pigId = self.pigId;
    }
    return _viewModel;
}

@end
